package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.model.Model1;
import com.sam.im.heiqun.entities.model.Model4;


public class CountryExpandAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    private LayoutInflater inflater;

    private IChildItemClick iChildItemClick;
    private Model4 data ;

    public CountryExpandAdapter(Model4 data, Context context) {
        if (data==null) {
            data = new Model4();
        }
        this.data = data;
        this.inflater = LayoutInflater.from(context);
    }

    public void setiChildItemClick(IChildItemClick iChildItemClick) {
        this.iChildItemClick = iChildItemClick;
    }

    @Override
    public int getGroupCount() {
        return data.getDict().getKey().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.getDict().getArray().get(groupPosition).getDict().size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return data.getDict().getKey().get(groupPosition);
    }

    @Override
    public Model1 getChild(int groupPosition, int childPosition) {
        return data.getDict().getArray().get(groupPosition).getDict().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.country_group_item, parent, false);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.abc.setText(getGroup(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.country_child_item, parent, false);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        Model1 child = getChild(groupPosition, childPosition);
        holder.country.setText(child.getString().get(1));
        holder.mobile.setText("+" + child.getString().get(2));

        holder.country.setTag(new SModel(child.getString().get(1),child.getString().get(2)));
        holder.mobile.setTag(new SModel(child.getString().get(1),child.getString().get(2)));

        holder.country.setOnClickListener(this);
        holder.mobile.setOnClickListener(this);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public void onClick(View v) {
        if (iChildItemClick != null) {
            SModel sModel = (SModel) v.getTag();
            iChildItemClick.itemClickDo(sModel.getCountry(),sModel.getMobileCode());
        }
    }

    private class GroupHolder {
        TextView abc;

        GroupHolder(View view) {
            this.abc = (TextView) view.findViewById(R.id.group_abc);
        }
    }

    private class ChildHolder {
        TextView mobile;
        TextView country;

        ChildHolder(View view) {
            this.mobile = (TextView) view.findViewById(R.id.mobile);
            this.country = (TextView) view.findViewById(R.id.country);
        }
    }

    public interface IChildItemClick{
        void itemClickDo(String country,String mobileCode);
    }

    private class SModel{
        String country;
        String mobileCode;

        public SModel(String country, String mobileCode) {
            this.country = country;
            this.mobileCode = mobileCode;
        }

        public String getCountry() {
            return country;
        }

        public String getMobileCode() {
            return mobileCode;
        }
    }

}

